package com.ymatou.iapi.liveseller.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.ActivityProductDetail;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.GetLiveActivityProductsBean;
import com.ymatou.iapi.liveseller.parameter.LiveActivityProductDtoV2;
import com.ymatou.iapi.liveseller.service.GetLiveActivityProductsCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*获取直播秒杀商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetLiveActivityProducts{
	private static GetLiveActivityProductsBean getliveactivityproductsBean;
	private static GetLiveActivityProductsCall getliveactivityproductsCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取直播秒杀商品列表");
	}

	@Before
	public void caseUp() {
		getliveactivityproductsBean=new GetLiveActivityProductsBean();
		getliveactivityproductsCall=new GetLiveActivityProductsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getliveactivityproductsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetLiveActivityProducts_001() {
		Logger.start(true,"获取直播秒杀商品列表");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId1 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			String productId2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date starttime1 = new Date(now.getTime() + 60 * 70 * 1000);
			
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
		
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId1);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId2);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids1 = CIWapper.selectCatalogsByProductId(productId1).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			List<String> cids2 = CIWapper.selectCatalogsByProductId(productId2).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean1 = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids1.get(0), 5d, 6, starttime);
			CreateLiveActivityBean createliveactivityBean2 = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids2.get(0), 5d, 6, starttime1);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean1);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean2);
			
			Integer activityStock1 = createliveactivityBean1.getLiveActivity().getActivityProducts().get(0).getActivityStock();
			Integer activityStock2 = createliveactivityBean2.getLiveActivity().getActivityProducts().get(0).getActivityStock();
			Double aprice1 = createliveactivityBean1.getLiveActivity().getActivityProducts().get(0).getActivityPrice();
			Double aprice2 = createliveactivityBean2.getLiveActivity().getActivityProducts().get(0).getActivityPrice();
			
			List<Integer> list = new ArrayList<Integer>();
			list.add(createlive.getId());
			getliveactivityproductsBean.setLiveIdList(list);
			getliveactivityproductsCall.setData(getliveactivityproductsBean);
			getliveactivityproductsCall.callService();		
			
			String Code = getliveactivityproductsCall.getString("Code");
			Logger.verifyEquals("200", Code, "验证Code");
			
			LiveActivityProductDtoV2 liveActivityProductDto = getliveactivityproductsCall.getLiveActivityProducts().get(0);
			ActivityProductDetail activityProductDetail1 = getliveactivityproductsCall.getLiveActivityProducts().get(0).getActivityProducts().get(0);
			ActivityProductDetail activityProductDetail2 = getliveactivityproductsCall.getLiveActivityProducts().get(0).getActivityProducts().get(1);
			Logger.verifyEquals(createlive.getId(), liveActivityProductDto.getLiveId(), "验证LiveId");
			Logger.verifyEquals(2, liveActivityProductDto.getActivityProductCount(), "验证活动商品数");
			Logger.verifyEquals(productId1, activityProductDetail1.getProductId(), "验证商品id");
			Logger.verifyEquals(aprice1, activityProductDetail1.getLiveActivityPrice(), "验证活动价格");
			Logger.verifyEquals(activityStock1, activityProductDetail1.getLiveActivityStock(), "验证活动库存");
			
			Logger.verifyEquals(productId2, activityProductDetail2.getProductId(), "验证商品id");
			Logger.verifyEquals(aprice2, activityProductDetail2.getLiveActivityPrice(), "验证活动价格");
			Logger.verifyEquals(activityStock2, activityProductDetail2.getLiveActivityStock(), "验证活动库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}